/*
 * Author: Anthony Fleshner
 * 
 * ControlComponent.java:
 * Handles drawing the robot controls
 */


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.event.*;


public class ControlComponent extends JPanel{
    
    RobotComponent robot;
    JButton rotate3clock;
    JButton slideLeft;
    JRadioButton paintToggle;
    JButton rotate2counter;
    JButton slideRight;
    JButton clearDraw;
    JButton resetDraw;
    JRadioButton mouseToggle;

    JTabbedPane tabPane;
    
    
    public ControlComponent(RobotComponent rob){
    	
        this.robot = rob;
        
        this.setSize(new Dimension(130,500));
        this.setPreferredSize(new Dimension(130,500));
        this.setVisible(true);
        
        tabPane = new JTabbedPane();
        tabPane.setSize(new Dimension(130, 340));
        tabPane.setPreferredSize(new Dimension(130, 340));
        this.add(tabPane);
        createButtonsF();
        createButtonsI();

        // turn off mouse when the user switches back to forward kinematics
        tabPane.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent event) {
                JTabbedPane pane = (JTabbedPane) event.getSource();
                if (pane.getSelectedIndex() == 0 && mouseToggle.isSelected()) {
                    mouseToggle.doClick();
                }
            }
        });

        paintToggle = new JRadioButton("Paint");
        paintToggle.setFont(new Font("sansserif",Font.PLAIN,20));
        paintToggle.addActionListener(new ButtonListener(robot));
        this.add(paintToggle);

        clearDraw = new JButton("Clear");
        clearDraw.setFont(new Font("sansserif", Font.PLAIN, 15));
        clearDraw.addActionListener(new ButtonListener(robot));
        this.add(clearDraw);

        resetDraw = new JButton("Reset");
        resetDraw.setFont(new Font("sansserif", Font.PLAIN, 15));
        resetDraw.addActionListener(new ButtonListener(robot));
        this.add(resetDraw);
    }
    
    /**
     * Set up the buttons for the forward kinematics tab
     */
    public void createButtonsF(){
        rotate3clock = new JButton("3 CW");
        rotate3clock.setFont(new Font("sansserif",Font.PLAIN,15));
        rotate3clock.addActionListener(new ButtonListener(robot));
        
        JButton rotate2clock = new JButton("2 CW");
        rotate2clock.setFont(new Font("sansserif",Font.PLAIN,15));
        rotate2clock.addActionListener(new ButtonListener(robot));
        
        slideLeft = new JButton("Left");
        slideLeft.setFont(new Font("sansserif",Font.PLAIN,15));
        slideLeft.addActionListener(new ButtonListener(robot));
        
        JButton rotate3counter = new JButton("3 CCW");
        rotate3counter.setFont(new Font("sansserif",Font.PLAIN,15));
        rotate3counter.addActionListener(new ButtonListener(robot));
        
        rotate2counter = new JButton("2 CCW");
        rotate2counter.setFont(new Font("sansserif",Font.PLAIN,15));
        rotate2counter.addActionListener(new ButtonListener(robot));
        
        slideRight = new JButton("Right");
        slideRight.setFont(new Font("sansserif",Font.PLAIN,15));
        slideRight.addActionListener(new ButtonListener(robot));
        
        
        JPanel tmp = new JPanel();
        tmp.setLayout(new GridLayout(10, 1));
        tmp.add(new JLabel());
        tmp.add(rotate3counter);
        tmp.add(rotate3clock);
        tmp.add(new JLabel());
        tmp.add(rotate2counter);
        tmp.add(rotate2clock);
        tmp.add(new JLabel());
        tmp.add(slideLeft);
        tmp.add(slideRight);
        tmp.add(new JLabel());

        tabPane.addTab("For.", tmp);
    }

    /**
     * Set up the buttons for the inverse kinematics tab
     */
    public void createButtonsI() {
        JButton moveLeft = new JButton("L");
        moveLeft.setFont(new Font("sansserif", Font.PLAIN, 10));
        moveLeft.addActionListener(new ButtonListener(robot));

        JButton moveRight = new JButton("R");
        moveRight.setFont(new Font("sansserif", Font.PLAIN, 10));
        moveRight.addActionListener(new ButtonListener(robot));

        JButton moveUp = new JButton("U");
        moveUp.setFont(new Font("sansserif", Font.PLAIN, 10));
        moveUp.addActionListener(new ButtonListener(robot));

        JButton moveDown = new JButton("D");
        moveDown.setFont(new Font("sansserif", Font.PLAIN, 10));
        moveDown.addActionListener(new ButtonListener(robot));      

        mouseToggle = new JRadioButton("Mouse");
        mouseToggle.setFont(new Font("sansserif", Font.PLAIN, 12));
        mouseToggle.addActionListener(new ButtonListener(robot));

        JPanel tmp = new JPanel();
        tmp.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0; c.gridy = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 2;
        tmp.add(new JLabel(), c);
        c.ipady = 0;
        c.gridx = 0; c.gridy = 1;
        tmp.add(new JLabel(), c);
        c.gridx = 1; c.gridy = 1;
        tmp.add(moveUp, c);
        c.gridx = 2; c.gridy = 1;
        tmp.add(new JLabel(), c);
        c.gridx = 0; c.gridy = 2;
        tmp.add(moveLeft, c);
        c.gridx = 1; c.gridy = 2;
        tmp.add(moveDown, c);
        c.gridx = 2; c.gridy = 2;
        tmp.add(moveRight, c);
        c.gridx = 0; c.gridy = 3;
        c.ipady = 20;
        tmp.add(new JLabel(), c);
        c.ipady = 0;
        c.gridx = 0; c.gridy = 4;
        c.gridwidth = 3;
        tmp.add(mouseToggle, c);

        tabPane.addTab("Inv.", tmp);
    }

    public RobotComponent getRobot() {
        return robot;
    }
    
    public JRadioButton getPaintToggle(){
    	return paintToggle;
    }
    
    public void drawFace(){
        paintToggle.doClick();
        for (int x = 0; x < 365; x++){
            rotate3clock.doClick();
        }
        paintToggle.doClick();
        for (int x = 0; x < 15; x++){
            slideLeft.doClick();
        }
        paintToggle.doClick();
        for (int x = 0; x < 365; x++){
            rotate3clock.doClick();
        }
        paintToggle.doClick();
        for (int x = 0; x < 50; x++){
            rotate2counter.doClick();
        }
        paintToggle.doClick();
        for (int x = 0; x < 50; x++){
            rotate2counter.doClick();
            slideRight.doClick();
        }
    }
    
}
